package com.minelx.table.core.series.index;

import java.util.stream.LongStream;

public class SwapIndexes implements Indexes {
	private final Indexes decorated;

	private final long between;

	private final long and;

	public SwapIndexes(Indexes decorated, long between, long and) {
		this.decorated = decorated;
		this.between = between;
		this.and = and;
	}

	@Override
	public long at(long index) {
		return decorated.at(mapIndex(index));
	}

	@Override
	public long len() {
		return decorated.len();
	}

	@Override
	public LongStream stream() {
		return decorated.stream().map(this::mapIndex);
	}

	private long mapIndex(long index) {
		if (index == between) {
			return and;
		} else if (index == and) {
			return between;
		} else {
			return index;
		}
	}
}
